﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Sdari.Api.DbModel;
using System.Linq;
using System.Threading.Tasks;

namespace Sdari.Api.Gateway.Controllers
{
    [Authorize]
    public class CallingLogController : Controller
    {
        private readonly ITokenServiceContext _db;
        private readonly ILogger<CallingLogController> _logger;

        public CallingLogController(ITokenServiceContext context, ILogger<CallingLogController> logger)
        {
            _db = context;
            _logger = logger;
        }

        // GET: CallingLogs
        public async Task<IActionResult> Index(int limit = 100, int start = 0)
        {
            var tokenDbConnection = _db.CallingLog.Include(c => c.ApiU).Include(c => c.ConsumerU).Include(c => c.WhiteListU)
                .OrderByDescending(o => o.CreateDate)   //必须写前面，否则会先截取再排序
                .Skip(start)    //需要配置对SQL Server2008的支持
                .Take(limit);
            return View(await tokenDbConnection.ToListAsync());
        }

        /*
        // GET: CallingLogs/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var callingLog = await _context.CallingLogs
                .Include(c => c.ApiU)
                .Include(c => c.ConsumerU)
                .Include(c => c.WhiteListU)
                .FirstOrDefaultAsync(m => m.Id == id);
            if (callingLog == null)
            {
                return NotFound();
            }

            return View(callingLog);
        }

        // GET: CallingLogs/Create
        public IActionResult Create()
        {
            ViewData["ApiUid"] = new SelectList(_context.ApiInfos, "Uid", "Address");
            ViewData["ConsumerUid"] = new SelectList(_context.ConsumerInfos, "Uid", "Secret");
            ViewData["WhiteListUid"] = new SelectList(_context.ApiWhitelists, "Uid", "Uid");
            return View();
        }

        // POST: CallingLogs/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,ApiUid,ConsumerUid,OperationTypeId,WhiteListUid,Result,Description,Creator,CreateDate,LastUpdater,LastUpdatedDate")] CallingLog callingLog)
        {
            if (ModelState.IsValid)
            {
                _context.Add(callingLog);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            ViewData["ApiUid"] = new SelectList(_context.ApiInfos, "Uid", "Address", callingLog.ApiUid);
            ViewData["ConsumerUid"] = new SelectList(_context.ConsumerInfos, "Uid", "Secret", callingLog.ConsumerUid);
            ViewData["WhiteListUid"] = new SelectList(_context.ApiWhitelists, "Uid", "Uid", callingLog.WhiteListUid);
            return View(callingLog);
        }

        // GET: CallingLogs/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var callingLog = await _context.CallingLogs.FindAsync(id);
            if (callingLog == null)
            {
                return NotFound();
            }
            ViewData["ApiUid"] = new SelectList(_context.ApiInfos, "Uid", "Address", callingLog.ApiUid);
            ViewData["ConsumerUid"] = new SelectList(_context.ConsumerInfos, "Uid", "Secret", callingLog.ConsumerUid);
            ViewData["WhiteListUid"] = new SelectList(_context.ApiWhitelists, "Uid", "Uid", callingLog.WhiteListUid);
            return View(callingLog);
        }

        // POST: CallingLogs/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("Id,ApiUid,ConsumerUid,OperationTypeId,WhiteListUid,Result,Description,Creator,CreateDate,LastUpdater,LastUpdatedDate")] CallingLog callingLog)
        {
            if (id != callingLog.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(callingLog);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CallingLogExists(callingLog.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            ViewData["ApiUid"] = new SelectList(_context.ApiInfos, "Uid", "Address", callingLog.ApiUid);
            ViewData["ConsumerUid"] = new SelectList(_context.ConsumerInfos, "Uid", "Secret", callingLog.ConsumerUid);
            ViewData["WhiteListUid"] = new SelectList(_context.ApiWhitelists, "Uid", "Uid", callingLog.WhiteListUid);
            return View(callingLog);
        }

        // GET: CallingLogs/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var callingLog = await _context.CallingLogs
                .Include(c => c.ApiU)
                .Include(c => c.ConsumerU)
                .Include(c => c.WhiteListU)
                .FirstOrDefaultAsync(m => m.Id == id);
            if (callingLog == null)
            {
                return NotFound();
            }

            return View(callingLog);
        }

        // POST: CallingLogs/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var callingLog = await _context.CallingLogs.FindAsync(id);
            _context.CallingLogs.Remove(callingLog);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool CallingLogExists(int id)
        {
            return _context.CallingLogs.Any(e => e.Id == id);
        }
        */
    }
}
